package com.ziyun.erp.modules.service.service.impl;

import com.service.common.protocol.ResultCode;
import com.service.order.api.OrderService;
import com.service.order.constant.OrderResult;
import com.service.order.model.order.OrderCancelParamDto;
import com.ziyun.erp.common.e.service.ServiceRefundStatusEnum;
import com.ziyun.erp.common.e.service.ServiceStatusEnum;
import com.ziyun.erp.modules.capital.entity.MerchantApplyCashEntity;
import com.ziyun.erp.modules.service.dto.ServiceRefundsConfirmDto;
import com.ziyun.erp.modules.service.entity.ServiceRegisterEntity;
import com.ziyun.erp.modules.service.service.ServiceRegisterService;
import io.renren.common.exception.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import com.ziyun.erp.modules.service.dao.ServiceRefundsDao;
import com.ziyun.erp.modules.service.entity.ServiceRefundsEntity;
import com.ziyun.erp.modules.service.service.ServiceRefundsService;
import org.springframework.transaction.annotation.Transactional;


@Service("serviceRefundsService")
public class ServiceRefundsServiceImpl extends ServiceImpl<ServiceRefundsDao, ServiceRefundsEntity> implements ServiceRefundsService {
    @Autowired
    private OrderService orderService;

    @Autowired
    private ServiceRegisterService serviceRegisterService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<ServiceRefundsEntity> page = new Query<ServiceRefundsEntity>(params).getPage();

        page.setRecords(this.baseMapper.selectServiceRefundsPage(page, params));

        return new PageUtils(page);
    }

    @Override
    public ServiceRefundsConfirmDto getConfirmRefundInfo(Integer srSrId) {
        return this.baseMapper.getConfirmRefundInfo(srSrId);
    }

    @Override
    @Transactional
    public void confirmRefund(ServiceRefundsEntity serviceRefunds) {
        ServiceRefundsEntity temp = this.selectById(serviceRefunds.getSrId());
        if (temp == null){
            throw new RRException("未查询到该退款单!");
        }

        if (temp.getSrRefundStatus().intValue() != ServiceRefundStatusEnum.SERVICE_RETURN_REFUND.toInt()){
            throw new RRException("当前退款单不支持退款操作!");
        }

        ServiceRegisterEntity serviceRegister = this.serviceRegisterService.selectById(temp.getSrSrId());
        if (serviceRegister == null){
            throw new RRException("未查询到该售后单!");
        }

        //1.将退款单状态置为 已退款
        serviceRefunds.setSrRefundStatus(ServiceRefundStatusEnum.SERVICE_EXCHANGE_GOODS.toInt());
        this.updateById(serviceRefunds);
        //2. 将售后单状态置为 已退款
        ServiceRegisterEntity updater = new ServiceRegisterEntity();
        updater.setSrId(serviceRegister.getSrId());
        updater.setSrStatus(ServiceStatusEnum.SERVICE_REFUND.toInt());
        this.serviceRegisterService.updateById(updater);


        //3. 请求微服务 取消订单
        OrderCancelParamDto orderCancelParamDto = new OrderCancelParamDto();
        orderCancelParamDto.setOrderId(serviceRegister.getSrOrderId());
        OrderResult<?> or =  this.orderService.updateOrderAfterSale(orderCancelParamDto);
        if (or.getCode() != ResultCode.SUCCESS.getCode()){
            throw new RRException("取消订单失败:" + or.getMessage());
        }

    }

}
